<!--
 * @Author: summer12138 1343364145@qq.com
 * @Date: 2023-05-20 20:24:35
 * @LastEditors: summer12138 1343364145@qq.com
 * @LastEditTime: 2023-05-20 21:10:50
 * @FilePath: \Vue核心技术与实战 - 作业\作业day02\全选和反选.html
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <div>
      <span>全选:</span>
      <input type="checkbox" v-model="isAll"/>
      <button @click="fn(isAll)">反选</button>
      <ul>
        <li v-for="(item) in arr">
          <input type="checkbox" v-model="item.checked" />
          <span>{{item.name}}</span>
        </li>
      </ul>
    </div>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
  <script>
    const app = new Vue({
      el: '#app',
      data: {
        arr: [
          {
            name: "猪八戒",
            checked: false,
          },
          {
            name: "孙悟空",
            checked: false,
          },
          {
            name: "唐僧",
            checked: false,
          },
          {
            name: "白龙马",
            checked: false,
          },
        ],
      },
      methods: {
          fn(flag){
          this.arr.forEach(item=>item.checked=!flag)
        }
      },
      computed: {
        isAll:{
          get(){
           return this.arr.every(item=>item.checked)
          },
          set(val){
            this.arr.forEach(item=>item.checked=val)
          }
        }
      },
    })
  </script>
</body>
</html>